"""
json数据的参数化
"""

# 导包
import json
import requests
import unittest
from parameterized import parameterized
from api.login import LoginAPI

# 构建参数化数据
def build_data():
    test_data = []
    file = "../data/login.json"
    with open(file, encoding="utf-8") as f:
        json_data = json.load(f)  # 加载json数据
        for case_data in json_data:
            username = case_data.get("username")
            password = case_data.get("password")
            verify_code = case_data.get("verify_code")
            status_code = case_data.get("status_code")
            status = case_data.get("status")
            content_type = case_data.get("content_type")
            msg = case_data.get("msg")
            test_data.append((username, password, verify_code, status_code, status, content_type, msg))  # 记住此处是一个双括号，因为内侧括号代表元组
            print(test_data)
        return test_data

# 创建测试类
class TestLogin1(unittest.TestCase):
    # 前置处理
    def setUp(self):
        self.login_api = LoginAPI()
        self.session = requests.Session()

    # 后置处理
    def tearDown(self):
        if self.session:
            self.session.close()

    # 创建测试用例
    @parameterized.expand(build_data())
    def test01_login_parameterized(self, username, password, verify_code, status_code, status, content_type, msg):
        # 调用验证码接口并断言
        response = self.login_api.get_verify_code(self.session)
        self.assertEqual(status_code, response.status_code)
        self.assertIn(content_type, response.headers.get("Content-Type"))

        # 调用登陆接口并断言
        response = self.login_api.login(self.session, username, password, verify_code)
        print(response.json())
        self.assertEqual(status_code, response.status_code)
        self.assertEqual(status, response.json().get("status"))
        self.assertIn(msg, response.json().get("msg"))
